package com.lwlk.zdk.controller.Binding;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.model.pw.pwPvBinding;
import com.lwlk.zdk.service.Binding.BindingService;
import com.lwlk.zdk.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Transactional
@RequestMapping(value = "/binding")
public class BindingController {


    private final static String port = PropUtils.getString("ports");
    private final static String Ip = PropUtils.getString("Ip");
    private final static String clientCode = PropUtils.getString("clientCode");

    @Autowired
    BindingService bindingService;


    @ApiOperation(value = "车辆和接入平台绑定接口(添加)")
    @RequestMapping(value = "/addList",method = RequestMethod.POST)
    public Response getAddList(Integer platformId,Integer vehicleId){
         Response response=new Response();
         try {
             Map map=new HashMap();
             map.put("vehicleId",String.valueOf(vehicleId));//必选 车辆id
             map.put("platformId",String.valueOf(platformId));//必选 平台id
             String url= "http://"+Ip+":"+port+"/dis/business/vehicle/bindplatform";
             Map<String,String>headMap=new HashMap<>();
             headMap.put("clientCode",clientCode);
             String result= HttpClientUtils.doPost(url,headMap,map);
             System.out.println(result);
                 pwPvBinding pwPvBinding=new pwPvBinding();
                 pwPvBinding.setPlatformId(platformId);
                 pwPvBinding.setVehicleId(vehicleId);
                 int i=bindingService.addList(pwPvBinding);
                 response.setData(i);
                 response.setCode(RetCode.SUCCESS);
                 response.setMsg("执行成功");
         }catch (Exception e){
             e.printStackTrace();
         }
         return response;
    }

    @ApiOperation(value = "平台与车辆绑定(展示)")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "页码",paramType = "query",required=true),
            @ApiImplicitParam(name = "size",value = "页面记录数",paramType = "query",required=true)})
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public LayuiTablePagingResult getShow(Integer page, Integer size){
        LayuiTablePagingResult result=null;
        try {
            PageHelper.startPage(page,size);
            List<Map<String,Object>>bindingList =bindingService.selectAll();
            PageInfo pageInfo = new PageInfo(bindingList);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
            Long totalPages = pageInfo.getTotal() / size;
            if (pageInfo.getTotal() % size != 0){
                totalPages ++;
            }
            result.setTotalPages(totalPages);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @ApiOperation(value = "平台与车辆绑定(删除)")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Response getDelete(Integer[] ids){
        Response response=new Response();
        for (Integer id : ids) {
            int i=bindingService.delete(id);
        }
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }


}
